#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sqlite3
from multiprocessing import Pool

db_path = "/tmp/test.db"
if os.path.exists(db_path):
    os.remove(db_path)

conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute("CREATE TABLE table_1 (id int, output int)")
c.execute("CREATE TABLE table_2 (id int, output int)")
c.execute("CREATE TABLE table_3 (id int, output int)")


def write_to_file(num):
    conn = sqlite3.connect(db_path)
    c = conn.cursor()
    sql = f'INSERT INTO table_{num} (id, output)' 'VALUES (?,?)'
    for i in range(1000000):
        c.execute(sql, (1, i + 1))
    conn.commit()
    # c.close()
    # conn.close()
    print(f"write table_{num} done")


if __name__ == "__main__":
    p = Pool(3)
    p.map(write_to_file, [1, 2, 3])
    p.close()
    p.join()

    # if os.path.exists(db_path):
    #     os.remove(db_path)
